/*-----------[http]-----------*/
let http = {
    // 获取url参数 http://xxx.com?name=1  传入name ->> 1
    getParam: function (key) {
        let tmp = decodeURIComponent(location.search);
        let index = tmp.indexOf(key);
        let len = key.length;
        let value = '';
        if (-1 != index) {
            let start = index + len + 1;
            let end = tmp.indexOf('&', start);
            if (end == -1) {
                value = tmp.slice(start);
            } else {
                value = tmp.slice(start, end);
            }
        }
        return value;
    }
};

/*-----------[layer弹窗]-----------*/
let win = {
    open: function(title, content, id){
        return layer.open({
            type: 2,
            title: title,
            area: ['550px', '550px'],
            content: content,
            btn: ['提交', '重置', '关闭'],
            yes: function (index, layero) {
                layero.find('iframe').contents().find('#LAY-form-submit').click();
                return false; //禁止点击该按钮关闭
            },
            btn2: function (index, layero) {
                return false;
            },
            success: function (layero, index) {
                $('#LAY-form-display').removeClass('layui-hide');
                // 触发详情
                if (id){
                    // 给id标签赋值
                    let body = layer.getChildFrame('body', index);
                    body.find('[name="id"]').val(id);
                    // 触发按钮获取详情
                    let btn = layero.find('iframe').contents().find("#getBtn");
                    btn.click();
                };

                // 给装备id复制
                let body = layer.getChildFrame('body', index);
                if(body.find('[name="eqmId"]')){
                    const eqmId = http.getParam('eqmId'), type = http.getParam('type') == 'FINISH'? 1 : null;
                    body.find('[name="eqmId"]').val(eqmId);
                }

            }
        });
    },
    close: function (index) {
        layer.close(index);
    },
    editRemaining: function(id, remaining, url, tableIns){
        layer.prompt({
            formType: 0,
            value: remaining,
            title: '剩余材料数量'
        },function(value, index, elem){
            let params = {'id': id, 'remaining': value};
            $.post(url, JSON.stringify(params), function(res){
                if (res.code !== 0) {
                    layer.msg('操作出错:[' + res.message + ']', {icon: 5});
                } else {
                    layer.msg('操作成功', {icon: 6});
                    layer.close(index);
                    tableIns.reload();
                }
            });
        });
    },
    editPrice: function(id, price, url, tableIns){
        layer.prompt({
            formType: 0,
            value: price,
            title: '当前价格'
        },function(value, index, elem){
            let params = {'id': id, 'price': value};
            $.post(url, JSON.stringify(params), function(res){
                if (res.code !== 0) {
                    layer.msg('操作出错:[' + res.message + ']', {icon: 5});
                } else {
                    layer.msg('操作成功', {icon: 6, time:1000});
                    layer.close(index);
                    tableIns.reload();
                }
            });
        });
    },
    eqm: function(title, content){
        return layer.open({
            type: 2,
            title: title,
            area: ['1050px', '550px'],
            content: content,
            btn: ['关闭'],
            yes: function (index, layero) {
                layer.close(index);
                // tableIns.render();
            },
            success: function (layero, index) {

            }
        });
    }
};